# -*- coding = utf-8 -*-
# @Time    : 2025/4/10 下午6:58
# @Author  : yqk
# @File    : 爬取飞卢小说网免费小说.py
# @Software: PyCharm
import re
import time
import random

import requests
from bs4 import BeautifulSoup
# headers = {
#     'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
#     'Accept-Language': 'zh-CN,zh;q=0.9',
#     'Cache-Control': 'max-age=0',
#     'Connection': 'keep-alive',
#     'Referer': 'https://b.faloo.com/1217177_90.html',
#     'Sec-Fetch-Dest': 'document',
#     'Sec-Fetch-Mode': 'navigate',
#     'Sec-Fetch-Site': 'same-origin',
#     'Sec-Fetch-User': '?1',
#     'Upgrade-Insecure-Requests': '1',
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0',
#     'sec-ch-ua': '"Microsoft Edge";v="135", "Not-A.Brand";v="8", "Chromium";v="135"',
#     'sec-ch-ua-mobile': '?0',
#     'sec-ch-ua-platform': '"Windows"',
# }
headers = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Cache-Control": "max-age=0",
    "Connection": "keep-alive",
    "Sec-Fetch-Dest": "document",
    "Sec-Fetch-Mode": "navigate",
    "Sec-Fetch-Site": "none",
    "Sec-Fetch-User": "?1",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36",
    "sec-ch-ua": "\"Google Chrome\";v=\"135\", \"Not-A.Brand\";v=\"8\", \"Chromium\";v=\"135\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\""
}
cookies = {
    "novelrelative": "1217177",
    "readline": "1",
    "fontFamily": "1",
    "fontsize": "16",
    "vip_img_width": "3",
    "font_Color": "666666",
    "Hm_lvt_6d308f6626f6d0864b6bb4f348f2b5e5": "1744286537",
    "HMACCOUNT": "E85503690916CA6F",
    "host4chongzhi": "b.faloo.com",
    "favorates28": "1217177%2C11",
    "autobuychapters28": "1217177%2C11",
    "bgcolor": "%23FFFFFE",
    "curr_url": "https%3A//b.faloo.com/html_1217_1217177/",
    "Hm_lpvt_6d308f6626f6d0864b6bb4f348f2b5e5": "1744338075"
}



# proxies = {
#     "http": "http://127.0.0.1:7897",
#     "https": "http://127.0.0.1:7897"
# }

url = 'https://b.faloo.com/1217177_1.html'
def get_content(url):
    # r = requests.get(url,headers=headers,proxies=proxies)
    r = requests.get(url,headers=headers,cookies=cookies)
    print(r.text)
    soup = BeautifulSoup(r.text,'html.parser')
    content = soup.find('div',class_='noveContent').text
    content = re.sub('【.*','',content).strip()
    return content

def get_links(index_url):
    # r = requests.get(index_url,headers=headers,proxies=proxies)
    r = requests.get(index_url,headers=headers,cookies=cookies)
    soup = BeautifulSoup(r.text,'html.parser')
    info = soup.find_all('a',target='_self')
    links = []
    titles = []
    for a in info:
        href = 'https:' + a.get('href')
        title = a.get('title')
        links.append(href)
        titles.append(title)
    file_name = soup.find('h1').text.strip()
    return links,titles,file_name

def download(link,title,file_name,path):
    content = get_content(link)
    # print(content)
    #
    with open(f'{path}/{file_name}.txt','a',encoding='utf-8') as f:
        f.write(title + '\n\n')
        f.write(content + '\n\n')
        f.write('--'*40+'\n\n')
        print(f'已下载...{title}')
        # time.sleep(random.uniform(1, 3))

if __name__ == '__main__':
    index_url = 'https://b.faloo.com/html_1217_1217177/'
    path = r'D:\python-code\python-spider\py-spider\02-python爬虫进阶\day20\novel'
    # get_links(index_url)
    links,titles,file_name = get_links(index_url)
    # get_content(url)
    for link,title in zip(links,titles):
        download(link,title,file_name,path)